package com.xesj.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhcacheUtil{

	private static Log log = LogFactory.getLog(EhcacheUtil.class);
	private static CacheManager cacheManager = null;
	private static EhcacheUtil ehcacheUtil = null;
	
	public EhcacheUtil(){
		cacheManager = CacheManager.create(EhcacheUtil.class.getClassLoader().getResourceAsStream("shiro-ehcache.xml"));
	}
	
	public static EhcacheUtil getInstance(){
		synchronized (EhcacheUtil.class) {
			if (ehcacheUtil == null) {  
				ehcacheUtil = new EhcacheUtil();
		    }  
		    return ehcacheUtil; 
		}
	}
	
	
	/**
	 * 
	* @Title: putObject
	* @Description: 设置缓存
	* @param @param cacheName 缓存的名称
	* @param @param key       缓存的键
	* @param @param value     缓存的值
	* @return void    返回类型
	* @throws
	 */
	public void putObject(Object key, Object value){
		Cache cache = cacheManager.getCache(Const.EHCACHE_NAMESPACE);
		if (cache == null){
			cache = new Cache(Const.EHCACHE_NAMESPACE, 10000000, true, true, 7200, 7200);
			cacheManager.addCache(cache);
		}
		Element element = new Element(key ,value);
		element.setEternal(true);
		element.setTimeToIdle(0);
		element.setTimeToLive(0);
		cache.put(element);
	}
	
	/**
	 * 
	* @Title: putExObject
	* @Description: 设置缓存(可设置存活的时间)
	* @param @param cacheName 缓存的名称
	* @param @param key       缓存的键
	* @param @param value     缓存的值
	* @param @param second    存活时间（秒）
	* @return void    返回类型
	* @throws
	 */
	public void putExObject(Object key, Object value, int second){
		Cache cache = cacheManager.getCache(Const.EHCACHE_NAMESPACE);
		if (cache == null){
			cache = new Cache(Const.EHCACHE_NAMESPACE, 10000000, false, false, second, second);
			cacheManager.addCache(cache);
		}
		Element element = new Element(key ,value);
		element.setEternal(false);
		element.setTimeToIdle(second);
		element.setTimeToLive(second);
		cache.put(element);
	}
	
	/**
	 * 
	* @Title: getObject
	* @Description: 根据key获取缓存中数据
	* @param @param cacheName 缓存的名称
	* @param @param key		      缓存的键
	* @param @return    设定文件
	* @return Object    返回类型
	* @throws
	 */
	public Object getObject(Object key){
		Cache cache = cacheManager.getCache(Const.EHCACHE_NAMESPACE);
		if (cache != null){
			Element element = cache.get(key);
			return element == null ? null : element.getObjectValue();
		}
		return null;
	}
	
	/**
	 * 
	* @Title: getCache
	* @Description: 根据名称获取缓存
	* @param @param cacheName 缓存的名称
	* @param @return    设定文件
	* @return Cache    返回类型
	* @throws
	 */
	public Cache getCache(String cacheName){
		return cacheManager.getCache(cacheName);
	}
	
	/**
	 * 
	* @Title: removeObject
	* @Description: 删除某个缓存数据
	* @param @param cacheName 缓存的名称
	* @param @param key       缓存的Key
	* @return void    返回类型
	* @throws
	 */
	public void removeObject(String key){
		Cache cache = cacheManager.getCache(Const.EHCACHE_NAMESPACE);
		if (cache != null){
			cache.remove(key);
		}
	}
	
	/**
	 * 
	* @Title: removeCache
	* @Description: 根据缓存名称删除缓存
	* @param @param cacheName    缓存的名称
	* @return void    返回类型
	* @throws
	 */
	public void removeCache(String cacheName){
		Cache cache = cacheManager.getCache(cacheName);
		if (cache != null){
			cacheManager.removeCache(cacheName);
		}
	}
	
	
	public static void main(String[] args) {
		/*EhcacheUtil.getInstance().putObject("TEST", "A", "a");
		EhcacheUtil.getInstance().putObject("TEST2", "B", "b");*/
		
		System.out.println(EhcacheUtil.getInstance().getObject("A"));
		System.out.println(EhcacheUtil.getInstance().getObject("B"));
		
		/*EhcacheUtil.getInstance().removeCache("TEST");
		EhcacheUtil.getInstance().removeCache("TEST2");*/
		
		
		System.out.println(EhcacheUtil.getInstance().getObject("A"));
		System.out.println(EhcacheUtil.getInstance().getObject("B"));
		
		/*EhcacheUtil.getInstance().putObject("testTime", "A");
		EhcacheUtil.getInstance().putObject("testTime1", "B" );*/
		System.out.println( "1--->" + EhcacheUtil.getInstance().getObject("testTime" ) );
		//Thread.sleep( 10000 );
		System.out.println( "2--->" + EhcacheUtil.getInstance().getObject("testTime1" ) );
		
		/*EhcacheUtil.getInstance().putExObject("testTime3", "A", 30);
		EhcacheUtil.getInstance().putExObject("testTime4", "B", 30);*/
		System.out.println( "3--->" + EhcacheUtil.getInstance().getObject("testTime3" ) );
		//Thread.sleep( 10000 );
		System.out.println( "4--->" + EhcacheUtil.getInstance().getObject("testTime4" ) );
	}
}
